const file = require('../../tool/file');//文件读写工具
//项目配置
let config = require('../config');
//截图保存目录
let {openScreen, imgPath, imgPathFiling, imgPathTribunal, imgPathCourt, imgPathAdjudication} = config.common;
//截图保存目录数组
let paths = [imgPath, imgPathFiling, imgPathTribunal, imgPathCourt, imgPathAdjudication];
//目录是否初始化
let isInit = false;

/**
 * 截图
 * @param page Puppeteer框架的页面
 * @param name 图片名称
 * @param index paths的索引
 */
async function screen(page, name = 'test', index = 0) {
  this.init = init;

  if (!openScreen) {
    return;
  }
  if (!isInit) {
    await init();
  }

  await page.screenshot({path: paths[index] + name + '.png'});
}

/**
 * 初始化截图目录
 */
async function init() {
  for (let item of paths) {
    await file.createDir(item, 1);
  }
  isInit = true;
}

module.exports = screen;